package cn.webrx.bs.servlet;

import cn.webrx.bs.entity.Book;
import cn.webrx.bs.service.BookCatesDao;
import cn.webrx.bs.service.BookCatesImpl;
import cn.webrx.bs.service.BookDao;
import cn.webrx.bs.service.BookDaoImpl;
import cn.webrx.bs.utils.ImgUtil;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;

/**
 * <p>功能描述: </p>
 *
 * @author webrx
 * @version 1.0
 * @date 2020-08-31 11:33
 */
@WebServlet("/admin/book") @MultipartConfig
public class AdminBook extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String action = req.getParameter("action");
        switch(action){
            case "add":
                BookCatesDao bd = new BookCatesImpl();
                req.setAttribute("cs",bd.queryAll());
                req.getRequestDispatcher("/admin/bookadd.ftl").forward(req, resp);
                break;

            case "save":
                //批量文件上传
                Collection<Part> ps = req.getParts();
                List<String> list = new ArrayList<>();
                for(Part p : ps){
                    if(p.getContentType() == null || p.getSize()==0) continue;
                    String path = req.getServletContext().getRealPath("/upload/book/");
                    String name = p.getSubmittedFileName().toLowerCase();
                    String ext = name.substring(name.lastIndexOf("."));
                    String nn = UUID.randomUUID().toString().toLowerCase() + ext;
                    //默认800_800
                    p.getInputStream().transferTo(new FileOutputStream(path+"/"+nn));
                    ImgUtil iu = new ImgUtil(path + "/" + nn);
                    iu.thumb(400,400,false,false);
                    iu.thumb(160,160,false,false);
                    iu.thumb(50,50,false,false);
                    iu.text("BookShop.cn（书城）",50,"red",5,true,10,"");

                    //生成400_400

                    //生成50_50
                    list.add(nn);

                }
                //数据保存到数据库


                Book book = new Book();
                book.setImges(JSON.toJSON(list).toString());
                book.setName(req.getParameter("name"));
                book.setCateid(Integer.parseInt(req.getParameter("cateid")));
                book.setNum(Integer.parseInt(req.getParameter("num")));
                book.setPrice(Double.parseDouble(req.getParameter("price")));
                book.setNprice(Double.parseDouble(req.getParameter("nprice")));
                book.setAuthor(req.getParameter("author"));
                book.setContent(req.getParameter("content"));
                book.setIntro(req.getParameter("intro"));
                book.setOnline(true);

                BookDao bdao = new BookDaoImpl();
                bdao.add(book);

                break;

        }

    }
}
